<template>
  <div class="todo-footer">
    <label>
      <input type="checkbox"
             :checked="isAllChecked"
             @click="isAllChecked = !isAllChecked"
      />
    </label>
    <span>
      <span>已完成{{ complateNum }}</span>
      / 全部{{ totalNum }}
    </span>
    <button
        class="btn btn-danger"
        @click="props.delALlTodo"
    >清除已完成任务
    </button>
  </div>
</template>

<script lang="ts">
import {defineComponent} from "vue";

export default defineComponent({
  name: "Footer",
})
</script>
<script lang="ts" setup>
import {TodosType} from "../modules/TodoList";
import {computed} from "vue";


//接收值

interface PropType {
  todoList: TodosType,
  delALlTodo: void
}

const props = defineProps<PropType>()


// * 全部完成与已完成数量

const totalNum = computed(() => {
  return props.todoList.length
})
const complateNum = computed(() => {
  return props.todoList.reduce((p, c) => c.done ? p + 1 : p, 0)
})


//* 全选逻辑

const isAllChecked = computed({
  get() {
    if (props.todoList.length === 0) return false
    return props.todoList.every((item) => item.done)

  },
  set(bool) {
    props.todoList.forEach((item) => item.done = bool)
  }
})


</script>

<style scoped>

.todo-footer {
  height: 40px;
  line-height: 40px;
  padding-left: 6px;
  margin-top: 5px;
}

.todo-footer label {
  display: inline-block;
  margin-right: 20px;
  cursor: pointer;
}

.todo-footer label input {
  position: relative;
  top: -1px;
  vertical-align: middle;
  margin-right: 5px;
}

.todo-footer button {
  float: right;
  margin-top: 5px;
  height: 40px;
  width: 135px;
  background: #ff3f00;
  color: white;
  font-weight: 900;
  border: 1px #ff3f00 solid;
  border-radius: 5%;
}
</style>
